package ClientServer;

import ocsf.client.*;

import java.awt.BorderLayout;
import java.io.*;
import java.sql.ResultSet;

import client.ClientConsole;

import GUI.ClientSideGUI;
import GUI.ConsoleGUI;
import GUI.MainFrame;
import Log.Log;


public class ClientSide extends AbstractClient {
	/* Variables */
	private boolean bConnected = false;
	String host ; // gets the host ip
	int DEFUALT_PORT = ProtocolIF.DEFAULT_PORT;
	ProtocolIF clientUI; 
	static ClientSideGUI GetDataFromGUI;
	
	/**** -------- constructors ---------- ****/
	public ClientSide(String host, int port, ProtocolIF proto) {
		super(host, port);
		this.clientUI = proto;
		EstablishConnection();
		// TODO Auto-generated constructor stub
	}
	//------------methods-----------------
	
	
	
	@Override
	protected void handleMessageFromServer(Object msg) {
		// TODO Auto-generated method stub
		if (msg!=null){
			if (msg instanceof String)
				clientUI.display(msg.toString());
			else if (msg instanceof ResultSet){
				clientUI.printQuerey((ResultSet)msg);
			}
		}
	}
	
	public void handleMessageFromClientUI(String message)
	  {
	    try
	    {
	    	sendToServer(message);
	    }
	    catch(IOException e)
	    {
	    
	      clientUI.display
	        ("Could not send message to server.  Terminating client.");
	      quit();
	    }
	  }
	
	
	
	
	public void quit()
	  {
	    try
	    {
	      closeConnection();
	    }
	    catch(IOException e) {}
	    System.exit(0);
	  }
	

	
	public void EstablishConnection(){
		 try {
				openConnection();
				
			} catch (IOException e) {
				
				
				// TODO Auto-generated catch block
				Log.addError("Could not establish connection... ");
				e.printStackTrace();
			}
	}
	public boolean getisConnected(){
		return this.isConnected();
	}
	
	
	
	
	
}
